//
//  DebugMainViewCell.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/12.
//

#import "DebugMainViewCell.h"
#import "DebugItemManager.h"

@interface DebugMainViewCell ()

///主要图像
@property(nonatomic,strong) UIImageView *itemImageView;
///主要title
@property(nonatomic,strong) UILabel *itemLabel;

@end

@implementation DebugMainViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI
{
    [self addSubview:self.itemImageView];
    [self addSubview:self.itemLabel];
}
- (void)setData:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakself = self;
    [[DebugItemManager sharedInstance] getItemDetail:indexPath detailBlock:^(UIImage * _Nonnull itemImage, NSString * _Nonnull itemTitle, void (^ _Nonnull itemClickBlock)(void (^ _Nonnull)(UIViewController * _Nonnull, NSInteger, BOOL), void (^ _Nonnull)(void))) {
        weakself.itemImageView.image = itemImage;
        weakself.itemLabel.text = itemTitle;
    }];
}
- (UIImageView *)itemImageView
{
    if(!_itemImageView)
    {
        _itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, self.bounds.size.width - 60, self.bounds.size.width - 60)];
        _itemImageView.contentMode = UIViewContentModeScaleAspectFit;
        if (@available(iOS 13.0, *)) {
            _itemImageView.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
                if ([traitCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
                    return [UIColor clearColor];
                }
                else {
                    return [UIColor lightGrayColor];
                }
            }];
        } else {
            _itemImageView.backgroundColor = UIColor.clearColor;
        }
        _itemImageView.clipsToBounds = YES;
    }
    return _itemImageView;
}
- (UILabel *)itemLabel
{
    if (!_itemLabel) {
        _itemLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.itemImageView.frame), self.bounds.size.width, 20)];
        _itemLabel.font = [UIFont systemFontOfSize:12];
        if (@available(iOS 13.0, *)) {
            _itemLabel.textColor = UIColor.labelColor;
        } else {
            _itemLabel.textColor = UIColor.blackColor;
        }
        _itemLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _itemLabel;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    [super traitCollectionDidChange:previousTraitCollection];
    [self layoutSubviews];
}

@end
